package com.chuangyang.fixboxmaster.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.CostItem;
import com.chuangyang.fixboxmaster.bean.Part;
import com.chuangyang.fixboxmaster.ui.fragment.BaseFragment;
import com.chuangyang.fixboxmaster.ui.fragment.orderprice.BrandFragment;
import com.chuangyang.fixboxmaster.ui.fragment.orderprice.PartFragment;
import com.chuangyang.fixboxmaster.ui.widgets.IconCenterEditText;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/6/10.
 */
public class ApplianceInfoActivity extends BaseActivity implements View.OnClickListener{


    public static final int APPLIANCE_BRAND_ACTIVITY = 0x000000;
    public static final int APPLIANCE_SPECIFICATION_ACTIVITY = 0x000001;
    public static final int APPLIANCE_PART_ACTIVITY = 0x000002;
    public static final String TYPE = "type";
    public static final String ORDER_ID = "order_id";
    public static final String BRAND_ID = " brand_id";
    public static final String TRANSCATION_ID = "transactionId";
    public static final String SPECIFICATION_ID = "specificationId";
    public static final String PRODUCT_DATE = "product_date";
    public static final String SELECT_PART = "select_part";
    public static final String SKILL_ID = "skill_id";
    private BaseFragment mFragment;
    private IconCenterEditText searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appliance_info);
        TextView partTV = (TextView)findViewById(R.id.part_ok);
        partTV.setOnClickListener(this);
        searchView = (IconCenterEditText)findViewById(R.id.part_search);
        int type = getIntent().getIntExtra(TYPE, -1);
        String orderId = getIntent().getStringExtra(ORDER_ID);
        String brandId = getIntent().getStringExtra(BRAND_ID);
        String mTransactionId = getIntent().getStringExtra(TRANSCATION_ID);
        String mSpecificationId = getIntent().getStringExtra(SPECIFICATION_ID);
        String mProductDate = getIntent().getStringExtra(PRODUCT_DATE);
        String mSkillId = getIntent().getStringExtra(SKILL_ID);
        switch (type) {
            case APPLIANCE_BRAND_ACTIVITY:
                setTitle("");
                partTV.setVisibility(View.INVISIBLE);
                mFragment = BrandFragment.newInstance();
                Bundle bundle = new Bundle();
                bundle.putString(ORDER_ID,orderId);
                mFragment.setArguments(bundle);
                beginTransaction(mFragment);
                enableDisableSwipeRefresh(true);
                break;
            case APPLIANCE_SPECIFICATION_ACTIVITY:
                setTitle("");
                partTV.setVisibility(View.INVISIBLE);
                mFragment = BrandFragment.newInstance();
                bundle = new Bundle();
                bundle.putString(BRAND_ID,brandId);
                bundle.putString(SKILL_ID,mSkillId);
                mFragment.setArguments(bundle);
                beginTransaction(mFragment);
                enableDisableSwipeRefresh(true);
                break;

            case APPLIANCE_PART_ACTIVITY:
                setTitle("");
                partTV.setVisibility(View.VISIBLE);
                bundle = getIntent().getExtras();
                Object object = bundle.getSerializable(SELECT_PART);
                CostItem item = (CostItem)bundle.getSerializable(ModuleActivity.REPAIR_TYPE);
                ArrayList<CostItem> costItems = (ArrayList<CostItem>)bundle.getSerializable(ModuleActivity.COST_ITEM);
                ArrayList<Part> mParts = null;
                if (object != null)
                {
                    mParts = (ArrayList<Part>)object;
                }
                mFragment = PartFragment.newInstance();
                bundle = new Bundle();
                bundle.putString(TRANSCATION_ID,mTransactionId);
                bundle.putString(SPECIFICATION_ID,mSpecificationId);
                bundle.putString(ORDER_ID,orderId);
                bundle.putString(PRODUCT_DATE,mProductDate);
                bundle.putString(BRAND_ID,brandId);
                if (mParts != null)
                    bundle.putSerializable(SELECT_PART,mParts);
                bundle.putSerializable(ModuleActivity.REPAIR_TYPE,item);
                bundle.putSerializable(ModuleActivity.COST_ITEM,costItems);
                mFragment.setArguments(bundle);
                beginTransaction(mFragment);
                enableDisableSwipeRefresh(true);
                break;
        }
        /*int type = getIntent().getIntExtra(TYPE, -1);
        String orderId = getIntent().getStringExtra(ORDER_ID);
        switch (type) {
            case ORDER_DETAL_ACTIVITY:
                setTitle(getResources().getString(R.string.title_activity_order_detail));
                fragment = new OrderDetailFragment();
                Bundle bundle = new Bundle();
                bundle.putString(ORDER_ID, orderId);
                fragment.setArguments(bundle);
                beginTransaction(fragment);
                enableDisableSwipeRefresh(true);
                break;
            case CASH_REQUEST_ACTIVITY:
                setTitle(getResources().getString(R.string.title_activity_cash_request));
                beginTransaction(CashRequestFragment.newInstance());
                enableDisableSwipeRefresh(false);
                break;
        }*/

    }

    private void beginTransaction(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
        searchView.addTextChangedListener(addTextWatch());
        searchView.setOnEditorActionListener(searchListener);
    }

    private TextWatcher addTextWatch(){
        if (mFragment instanceof BrandFragment)
        {

            return ((BrandFragment)mFragment).getTextWatcher();
        }
        else if (mFragment instanceof PartFragment)
        {
            return ((PartFragment)mFragment).getTextWatcher();
        }
        return null;
    }

    @Override
    protected void requestDataRefresh() {

        if (mFragment instanceof BrandFragment)
        {

            ((BrandFragment)mFragment).updateListView();
        }
        else if (mFragment instanceof PartFragment)
        {
            ((PartFragment)mFragment).updateListView();

        }
        onRefreshingStateChanged(false, 1000);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.part_ok:
                if (mFragment instanceof PartFragment)
                {
                    ((PartFragment)mFragment).addPart();
                }
                break;
        }

    }

    @Override
    public boolean canSwipeRefreshChildScrollUp() {
        if (mFragment instanceof BrandFragment || mFragment instanceof  PartFragment)
        {
            if(mFragment.getUserVisibleHint() && mFragment.getListView()!=null){
                return ViewCompat.canScrollVertically(mFragment.getListView(), -1);
            }

        }

        return false;
    }

    TextView.OnEditorActionListener searchListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
             /*判断是否是“SEARCH”键*/
            if(actionId == EditorInfo.IME_ACTION_SEARCH){
                                        /*隐藏软键盘*/
                InputMethodManager imm = (InputMethodManager) v
                        .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive()) {
                    imm.hideSoftInputFromWindow(
                            v.getApplicationWindowToken(), 0);
                }
                if (mFragment instanceof BrandFragment)
                {
                    ((BrandFragment)mFragment).doSearch();
                }
                else if (mFragment instanceof  PartFragment)
                {
                    ((PartFragment)mFragment).doSearch();
                }
                return true;
            }
            return false;
        }
    };

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }
}
